package com.changxianggu.cloud.crawler.core.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;


/**
 * @author liuyuantao
 */
@Slf4j
public class CrawlerThread implements Runnable {

    public static final String COOKIE = "Hm_lvt_c95a52817188b0d28b1d2f5c174f159c=1582679701,1582686604,1582774019,1582858336; PHPSESSID=ff8f467f3eb46e2b0b7c9435d5ba44de; Hm_lpvt_c95a52817188b0d28b1d2f5c174f159c=1582877431";
    public static final String ORIGIN = "http://www.changxianggu.com";
    public static final String REFERER = "http://www.changxianggu.com/teacher/ebook/index.html";

    public static final int pressId[] = {269, 3, 4, 5, 11, 12, 1, 2, 6};
    public static final String searchKey[] = {"学",
            "高等",
            "高等数学",
            "程序设计",
            "英语",
            "理",
            "设计",
            "原理",
            "线性代数",
            "数据库"};

    @SneakyThrows
    @Override
    public void run() {
        while (true) {
            String url = "http://www.changxianggu.com/teacher/ebook/search";
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Origin", ORIGIN);
            headers.put("Referer", REFERER);
            Map<String, Object> parameter = MapUtil.newHashMap(6);
            parameter.put("key", "学");
            parameter.put("press_id", pressId[RandomUtil.randomInt(0, 8)]);
            parameter.put("page", RandomUtil.randomInt(0, 1000));
            String result = null;
            try {
                result = HttpUtil.doPost(url, headers, parameter);
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info(result);
            log.info(Thread.currentThread().getName());
        }
    }
}
